
<div class="share-tpl hide">
    <p>分享链接: <span class="note-share-link-text"></span></p>
    <div class="row">
        <button class="note-share-link-btn btn" data-clipboard-text="">复制</button>
    </div>
</div>


<script type="text/javascript">
    // 移动笔记的回调函数
    window.moveNoteCallback = function (parentId) {
        var selfId = "{{file.id}}";
        $.post("/note/move", {id:selfId, parent_id: parentId}, function (resp){
              console.log(resp);
              window.location.reload();
        });
    }

    window.visitAndRefresh = function (el) {
        var url = $(el).attr("data-href");
        $.get(url, function () {
            window.location.reload();
        });
    }

    function renameNote(id, oldName) {
        xnote.prompt("新名称", oldName, function (newName) {
            console.log(newName);
            if (newName != "" && newName != null) {
                $.post("/note/rename", {id:id, name:newName}, function (resp) {
                    var code = resp.code;
                    if (code != "success") {
                        alert(resp.message);
                    } else {
                        // $("#file-"+id).text(newName);
                        window.location.reload();
                    }
                })
            }
        });
    }

    function renameNoteByAttr(target) {
        var id = $(target).attr("data-id");
        var oldName = $(target).attr("data-name");
        if (id == undefined || id == "") {
            xnote.alert("data-id为空");
            return;
        }

        if (oldName == undefined || oldName == "") {
            xnote.alert("data-name为空");
            return;
        }

        renameNote(id, oldName);
    }

    function checkNotEmpty(object, message) {
        if (object === undefined || object === null || object === "") {
            xnote.alert(message);
            throw new Error(message);
        }
    }

    function call_xnote_api(api, params) {
        $.post(api, params, function (resp) {
            if (resp.code != "success") {
                xnote.alert(resp.message);
            } else {
                xnote.toast(resp.message);
                window.location.reload();
            }
        }).fail(function (error) {
            console.error(error);
            xnote.alert("接口调用失败:" + error.status + "," + error.statusText);
        })
    }

    $(function () {

        function onNoteRenameEvent(event) {
            console.log("rename");
            var target = event.target;
            renameNoteByAttr(target);
        }
        
        $(".rename-btn").click(function(event) {
            onNoteRenameEvent(event);
        });

        $(".note-rename-btn").click(function(event) {
            onNoteRenameEvent(event);
        });

        $(".status-btn").each(function (index, element) {
            var status = $(element).attr("data-status");
            if (status == "{{file.priority}}") {
                $(element).addClass("x-tab-btn-active");
            }
        });

        $(".status-btn").click(function(event) {
            var id = $(event.target).attr("data-id");
            var status = $(event.target).attr("data-status");

            checkNotEmpty(id, "data-id为空");
            checkNotEmpty(status, "data-status为空");

            $.post("/note/status", {id: id, status: status}, function (resp) {
                var code = resp.code;
                if (code != "success") {
                    xnote.alert(resp.message);
                } else {
                    xnote.toast(resp.message);
                    window.location.reload();
                }
            })
        });

        $(".status-select").change(function(event) {
            var id = $(event.target).attr("data-id");
            var status = $(event.target).val();

            checkNotEmpty(id, "data-id为空");
            checkNotEmpty(status, "data-status为空");

            $.post("/note/status", {id: id, status: status}, function (resp) {
                var code = resp.code;
                if (code != "success") {
                    xnote.alert(resp.message);
                } else {
                    xnote.toast(resp.message);
                    window.location.reload();
                }
            })
        });

        $(".orderby-btn").each(function (index, element) {
            var orderby = $(element).attr("data-orderby");
            if (orderby == "{{file.orderby}}") {
                $(element).addClass("x-tab-btn-active");
            }
        });

        $(".orderby-btn").click(function (event) {
            var id = $(event.target).attr("data-id");
            var orderby = $(event.target).attr("data-orderby");

            checkNotEmpty(id, "data-id为空");
            checkNotEmpty(orderby, "data-orderby为空");

            $.post("/note/orderby", {id: id, orderby: orderby}, function (resp) {
                var code = resp.code;
                if (code != "success") {
                    xnote.alert(resp.message);
                } else {
                    xnote.toast(resp.message);
                    window.location.reload();
                }
            })
        });

        $(".orderby-select").change(function (event) {
            var id = $(event.target).attr("data-id");
            var orderby = $(event.target).val();

            checkNotEmpty(id, "data-id为空");
            checkNotEmpty(orderby, "data-orderby为空");

            $.post("/note/orderby", {id: id, orderby: orderby}, function (resp) {
                var code = resp.code;
                if (code != "success") {
                    xnote.alert(resp.message);
                } else {
                    xnote.toast(resp.message);
                    window.location.reload();
                }
            })
        });

        function note_open_share_dialog(target) {
            var id = $(target).attr("data-id");
            var type = "{{file.type}}";
            var params = {note_id: id};
            var ajax_dialog_url   = "/note/ajax/share_group_dialog";
            var ajax_dialog_title = "分享笔记本";

            if (type != "group") {
                ajax_dialog_url = "/note/ajax/share_note_dialog";
                ajax_dialog_title = "分享笔记";
            }

            $.get(ajax_dialog_url, params, function (resp) {
                xnote.showDialog(ajax_dialog_title, resp);
            });
        }

        $(".note-share-btn").click(function (event) {
            note_open_share_dialog(event.target);
        });

        function note_share_by_link(target) {
            /* Act on the event */
            $.post("/note/link_share", {id: "{{file.id}}"}, function (resp) {
                if (resp.code == "success") {
                    var link = window.location.protocol + "//" + window.location.host + resp.data;
                    $(".note-share-link-text").text(link);
                    $(".note-share-link-btn").attr("data-clipboard-text", link);

                    var html = $(".share-tpl").html();
                    layer.confirm(html, {
                        btn: [] //按钮
                    });
                    new ClipboardJS('.note-share-link-btn', {
                        text: function(trigger) {
                            xnote.toast("已经复制到粘贴板");
                            return trigger.getAttribute('data-clipboard-text');
                        }
                    });
                } else {
                    xnote.alert("分享失败!");
                }
            });
        }

        window.note_share_by_link = note_share_by_link;

        $(".copy-link-option").click(function(event) {
            $(".copy-link-option").attr('data-clipboard-text', window.location.href);
            new ClipboardJS(".copy-link-option", {
                text: function (trigger) {
                    xnote.toast("已经复制到粘贴板");
                    return trigger.getAttribute("data-clipboard-text");
                }
            });
        });

        // 显示目录弹窗
        $(".note-contents-btn").click(function (e) {
            var parentId = $(e.target).attr("data-parent");
            xnote.showAjaxDialog("选择笔记", "/note/" + parentId + "?dialog=true");
        });
    })
</script>

{% include note/component/script/delete_script.html %}